package com.jinzecloud.reportapproval.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzecloud.reportapproval.bean.ConsCheckRecordInfo;
import com.jinzecloud.reportapproval.bean.SpeedOperationTable;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import com.jinzecloud.reportapproval.service.ConsCheckRecordInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.ConsCheckRecord;
import com.jinzecloud.reportapproval.service.ConsCheckRecordService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 * 一致性检查记录表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 16:18:45
 */
@RestController
@RequestMapping("reportapproval/conscheckrecord")
public class ConsCheckRecordController {
    @Autowired
    private ConsCheckRecordService consCheckRecordService;

    @Autowired
    private ConsCheckRecordInfoService consCheckRecordInfoService;

    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:conscheckrecord:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = consCheckRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:conscheckrecord:info")
    public R info(@PathVariable("id") Integer id){
		ConsCheckRecord consCheckRecord = consCheckRecordService.getById(id);

        List<ConsCheckRecordInfo> consCheckRecordId = consCheckRecordInfoService.list(new QueryWrapper<ConsCheckRecordInfo>().eq("cons_check_record_id", consCheckRecord.getId()).orderByAsc("number"));

        consCheckRecord.setConsCheckRecordInfoList(consCheckRecordId);

        return R.ok().put("consCheckRecord", consCheckRecord);
    }

    @RequestMapping("/getInfo/{projectNumber}")
    //@RequiresPermissions("reportapproval:conscheckrecord:info")
    public R getInfo(@PathVariable("projectNumber") String projectNumber){
        ConsCheckRecord consCheckRecord = consCheckRecordService.getProductNumber(projectNumber);

        return R.ok().put("consCheckRecord", consCheckRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:conscheckrecord:save")
    public R save(@RequestBody ConsCheckRecord consCheckRecord){
        List<ConsCheckRecord> productNumber = consCheckRecordService.list(new QueryWrapper<ConsCheckRecord>().eq("product_number", consCheckRecord.getProductNumber()));
        if (productNumber.size() > 0) return R.error("编号："+consCheckRecord.getProductNumber()+"已存在，请勿重复添加！");
        consCheckRecord.setStatus(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        consCheckRecord.setUserId(adminService.getIdByName(authentication.getName()));
		return consCheckRecordService.addConsCheckRecord(consCheckRecord);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:conscheckrecord:update")
    public R update(@RequestBody ConsCheckRecord consCheckRecord){
		return consCheckRecordService.updateConsCheckRecord(consCheckRecord);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @Transactional
    //@RequiresPermissions("reportapproval:conscheckrecord:delete")
    public R delete(@RequestBody Integer[] ids){
		consCheckRecordService.removeByIds(Arrays.asList(ids));

        for (Integer id : ids) {
            List<ConsCheckRecordInfo> consCheckRecordId = consCheckRecordInfoService.list(new QueryWrapper<ConsCheckRecordInfo>().eq("cons_check_record_id", id));
            List<Integer> integers = new ArrayList<>();
            if (consCheckRecordId.size() > 0) {
                for (ConsCheckRecordInfo consCheckRecordInfo : consCheckRecordId) {
                    integers.add(consCheckRecordInfo.getId());
                }
            }
            consCheckRecordInfoService.removeByIds(integers);
        }
        return R.ok();
    }


    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return consCheckRecordService.viewPdf(id);
    }

    @RequestMapping("/viewWord/{id}")
    public String viewWord(@PathVariable("id") Integer id){
        return consCheckRecordService.viewWord(id);
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(consCheckRecordService.auditList(param));
    }

    @PostMapping("/review")
    public R review(@RequestBody ConsCheckRecord consCheckRecord) {
        consCheckRecordService.updateById(new ConsCheckRecord().setId(consCheckRecord.getId()).setStatus(StateEnum.CHECK_DETECT.getState()));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return consCheckRecordService.audit(auditParam);
    }

    @RequestMapping("/exportAll/{projectUuid}")
    public R exportAll (@PathVariable("projectUuid")String projectUuid){
        return consCheckRecordService.exportAll(projectUuid);
    }
}
